package com.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.data.DataBase;
import com.entity.ManagerPerson;

/**
 * 管理员的业务处理
 * 
 * @author YH
 *
 */
public class ManagerPersonService {

	static {
		/*// 添加一个默认的超级管理员
		ManagerPerson person = new ManagerPerson();
		person.setUsername("admin");
		person.setPassword("admin");
		person.setName("张三");
		person.setCreateDate(new Date());
		person.setSex(1);
		person.setAge(20);
		person.setRank(0);
		person.setAddress("陕西省1");
		DataBase.getManagerPersons().put(person.getUsername(), person);
		
		//添加几个普通管理员
		ManagerPerson person1 = new ManagerPerson();
		person1.setUsername("admin1");
		person1.setPassword("admin1");
		person1.setName("张三1");
		person1.setCreateDate(new Date());
		person1.setSex(1);
		person1.setAge(20);
		person1.setRank(1);
		person1.setAddress("陕西省2");
		DataBase.getManagerPersons().put(person1.getUsername(), person1);
		
		ManagerPerson person2 = new ManagerPerson();
		person2.setUsername("admin2");
		person2.setPassword("admin3");
		person2.setName("张三2");
		person2.setCreateDate(new Date());
		person2.setSex(0);
		person2.setAge(20);
		person2.setRank(1);
		person2.setAddress("陕西省2");
		DataBase.getManagerPersons().put(person2.getUsername(), person2);*/
		
		
		
	}

	// 添加
	public static boolean add(ManagerPerson ManagerPerson) {
		if(DataBase.getManagerPersons().containsKey(ManagerPerson.getUsername())){
			return false;
		}
		else{
			DataBase.getManagerPersons().put(ManagerPerson.getUsername(), ManagerPerson);
			return true;
		}	
		
	}

	// 删除
	public static void del(String username) {
		DataBase.getManagerPersons().remove(username);
	}

	// 改
	public static void update(ManagerPerson ManagerPerson) {
		DataBase.getManagerPersons().put(ManagerPerson.getUsername(), ManagerPerson);
	}

	// 查所有
	public static List<ManagerPerson> find() {
		Map<String, ManagerPerson> stus = DataBase.getManagerPersons();
		Set<String> kSet = stus.keySet();
		Object[] objs = kSet.toArray();
		List<ManagerPerson> list = new ArrayList<ManagerPerson>();
		for (int i = 0; i < objs.length; i++) {
			String sid = (String) (objs[i]);
			list.add(stus.get(sid));
		}
		return list;
	}

	// 查一个
	public static ManagerPerson findByUserName(String username) {
		return DataBase.getManagerPersons().get(username);
	}

	// 登录
	public static boolean login(String username, String password) {
		try {
			ManagerPerson stu = DataBase.getManagerPersons().get(username);
			if (password.equals(stu.getPassword())) {
				// 登录成功
				DataBase.getRequest().put("login", stu);
				return true;
			} else {
				// 登录失败
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}
}
